use std::sync::Arc;
use dashmap::DashMap;
use crate::{CmdStrategy, Redis, RespFrame, RespNull, Session, SimpleString};

pub struct DelCmd;


impl CmdStrategy for DelCmd {
    fn execute(&self, fragments: Vec<&str>, db: Redis, sesseion_id: String, sessions: Arc<DashMap<String, Session>>) -> Result<RespFrame, String> {
        let key = fragments[4];


        let s = sessions.get(&sesseion_id);

        let db_index = s.unwrap().db_index;

        let mut binding = db.databases[db_index].write().unwrap();
        let entry = binding.get(key);

        match entry {
            Some(v) => {

                binding.remove(key);
                let str = SimpleString::new("Ok");
                let resp = RespFrame::SimpleString(str);
                Ok(resp)
            }

            None => {
                Ok(RespFrame::Null(RespNull))
            }
        }
    }
}